<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\EduVideoCourse;

/**
 * EduVideoCourseSearch represents the model behind the search form of `backend\models\EduVideoCourse`.
 */
class EduVideoCourseSearch extends EduVideoCourse
{
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['user_id', 'label', 'is_free', 'learn_count', 'learn_base', 'sort', 'is_hot', 'is_recommend', 'create_time', 'update_time', 'status', 'video_time'], 'integer'],
            [['title', 'author', 'image', 'detail'], 'safe'],
            [['price', 'score'], 'number'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = EduVideoCourse::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => [
                'defaultOrder' => ['create_time' => SORT_DESC]
            ],
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'user_id' => $this->user_id,
            'label' => $this->label,
            'price' => $this->price,
            'is_free' => $this->is_free,
            'score' => $this->score,
            'learn_count' => $this->learn_count,
            'learn_base' => $this->learn_base,
            'sort' => $this->sort,
            'is_hot' => $this->is_hot,
            'is_recommend' => $this->is_recommend,
            'create_time' => $this->create_time,
            'update_time' => $this->update_time,
            'status' => $this->status,
            'evalation_count' => $this->evalation_count,
            'collect_count' => $this->collect_count,
            'collect_base' => $this->collect_base,
            'video_time' => $this->video_time,
        ]);

        $query->andFilterWhere(['like', 'title', $this->title])
            ->andFilterWhere(['like', 'author', $this->author])
            ->andFilterWhere(['like', 'image', $this->image])
            ->andFilterWhere(['like', 'video_url', $this->video_url])
            ->andFilterWhere(['like', 'detail', $this->detail]);

        return $dataProvider;
    }
}
